package stream;

import java.text.DecimalFormat;
import java.util.*;

public class ComparatorTest {
    public static void main(String[] args) {
        List<Map<String, String>> init = ComparatorTest.init();
        Map<String, Object> age = ComparatorTest.result(init, "age");
        for (String key:age.keySet()) {
            System.out.println(key+":"+age.get(key));
        }
    }
    public static List<Map<String, String>> init(){
        List<Map<String, String>> list=new ArrayList<Map<String, String>>();
        for (int i = 0; i <10 ; i++) {
            Map map=new HashMap();
            map.put("name","name"+i);
            map.put("age",i+"");
            list.add(map);
        }
        return list;
    }
    public static Map<String, Object> result(List<Map<String, String>> list, String dataType){
        Map<String, String> max = list.stream().max(Comparator.comparing(map -> String.valueOf(map.get(dataType)))).get();
        Map<String, String> min = list.stream().min(Comparator.comparing(map -> String.valueOf(map.get(dataType)))).get();
        double avg = list.stream().mapToDouble(map -> Double.parseDouble(String.valueOf(map.get(dataType)))).average().getAsDouble();
        Map<String, Object> result = new HashMap<>();
        DecimalFormat df = new DecimalFormat("#.##");
        result.put("max", max);
        result.put("min", min);
        result.put("avg", df.format(avg));
        return result;
    }
}
